package org.vsg.stock.basic.service.volfigure;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.vsg.stock.core.domain.StockDailyKLine;


public class IecreaseCounterManager {
	
	private static final HashMap<String,Integer> map = new HashMap<String,Integer>();
	
	private static final HashMap<String,Integer> atLeastmap = new HashMap<String,Integer>();

	private static final HashMap<String,Integer> starndmap = new HashMap<String,Integer>();	
	
	static private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");	
	
	static {
   	 	map.put("A+", 0);            	 
   	 	map.put("A", 0);
   	 	map.put("B", 0);
   	 	map.put("C", 0);
   	 	map.put("D", 0);
   	 	
   	 	atLeastmap.put("A+", 0);            	 
   	 	atLeastmap.put("A", 0);
   	 	atLeastmap.put("B", 0);
   	 	atLeastmap.put("C", 0);
   	 	atLeastmap.put("D", 0);
	}
	
	private static final ThreadLocal<HashMap<String,Integer>> mapCounter = 
         new ThreadLocal<HashMap<String,Integer>> () {
             @Override protected HashMap<String,Integer> initialValue() {
            	 
            	 HashMap<String,Integer> map = new HashMap<String,Integer>();
            	 map.put("A+", 0);            	 
            	 map.put("A", 0);
            	 map.put("B", 0);
            	 map.put("C", 0);
            	 map.put("D", 0);
            	 //map.put("E", 0);            	 
                 return map;
         }
     };
	
	
	/**
	 * 将增量柱的第一柱与最后一柱进行比较，建立三段位置，第一段，最高一段的最低价，第二，最低K最高与最高K最低，第三段是最低K是低，
	 * 第四段，是最低下2%以内的变化
	 * @param stkDailyKLineList
	 * @param index
	 * @param offset
	 * @param closeDate -- 最接近增量柱最大的最低量柱
	 */
	public static  void handle(List<StockDailyKLine> stkDailyKLineList, int index , int offset , Date closeDate) {
		
		
		
		// ---- count map object ---
		long lastVol = stkDailyKLineList.get( index ).getVol();
		StringBuffer keyStr = new StringBuffer();
		for (int i = 1 ; i <= offset ;i++) {
			long currentVol = stkDailyKLineList.get(index + i ).getVol();
			
			if ( currentVol * CommonParameter.SEPRATE_RATE > lastVol ) {
				keyStr.append("倍");
			} else {
				keyStr.append("增");
			}
			lastVol = currentVol;
		}
		Integer count = starndmap.get( keyStr.toString() );
		if ( keyStr.toString().equalsIgnoreCase( "增倍")) {
			System.out.println(stkDailyKLineList.get(index).getId().getStockCode() + " , " + stkDailyKLineList.get(index).getDate());
		}
		if ( count == null ) {
			count = 1;
		} else {
			count = count + 1;
		}
		
		starndmap.put( keyStr.toString() , count );
		//System.out.println( starndmap );
		
		// --- get the more value array ---
		double[] res = getLevelArray(stkDailyKLineList , index , offset);
		

/*		for (double i : res) {
			System.out.println(i);
		}*/
		
		
		// --- 分析最后价后三天的最低价 ---
		int begin = index + offset;
		int end = begin + 4;
		
		if ( end >= stkDailyKLineList.size() ) {
			return ;
		}
		
		// --- mark at least low ---
		StockDailyKLine tmpKLine = null;
		double tmpAtLeastPrice = 9999;
		for ( int i = begin+1 ; i < end ; i++) {
			tmpKLine = stkDailyKLineList.get(i);
			if ( tmpKLine.getLow() < tmpAtLeastPrice) {
				tmpAtLeastPrice = tmpKLine.getLow();
			}
		}
		
		// --- 分析离最低成交量之间的最低价 ---
		int j = 1 ;
		int newIndex = 0;
		Date tmpDate = null;
		double tAtLeastPrice = 9999;		
		do {
			
			newIndex = begin+j;
			tmpKLine = stkDailyKLineList.get(newIndex);
			tmpDate =  tmpKLine.getDate();
			
			if ( tmpKLine.getLow() < tAtLeastPrice ) {
				tAtLeastPrice = tmpKLine.getLow();
			}
			
			j+=1;
		} while ( closeDate != null && tmpDate.before( closeDate ) );
		
		//System.out.println("date : " + df.format( tmpDate ) + " , " +tAtLeastPrice + " , " + df.format( closeDate )  );
		
		// --- count to value ---
		//HashMap<String,Integer> mapValueMap = mapCounter.get();
		HashMap<String,Integer> mapValueMap = map;
		if ( res[0] != 0  ) {
			
			countMapValue(tmpAtLeastPrice , res , map);
			countMapValue(tAtLeastPrice , res , atLeastmap);

		}
	}

	public static  void handle2(List<StockDailyKLine> stkDailyKLineList, int index , int offset , Date closeDate) {
		
		
		
		// ---- count map object ---
		long lastVol = stkDailyKLineList.get( index ).getVol();
		StringBuffer keyStr = new StringBuffer();
		for (int i = 1 ; i <= offset ;i++) {
			StockDailyKLine stkLine = stkDailyKLineList.get(index + i );
			long currentVol = stkLine.getVol();
			double yieldRate = stkLine.getYieldRate();
			
			if ( currentVol * CommonParameter.SEPRATE_RATE > lastVol ) {
				keyStr.append("倍");

			} else {
				keyStr.append("增");
			}
			
			if ( yieldRate > 0) {
				keyStr.append("+");
			} else {
				keyStr.append("-");
			}
			
			lastVol = currentVol;
		}
		Integer count = starndmap.get( keyStr.toString() );
		if ( keyStr.toString().equalsIgnoreCase( "增倍")) {
			System.out.println(stkDailyKLineList.get(index).getId().getStockCode() + " , " + stkDailyKLineList.get(index).getDate());
		}
		if ( count == null ) {
			count = 1;
		} else {
			count = count + 1;
		}
		
		starndmap.put( keyStr.toString() , count );
		//System.out.println( starndmap );
		
		// --- get the more value array ---
		double[] res = getLevelArray(stkDailyKLineList , index , offset);
		

/*		for (double i : res) {
			System.out.println(i);
		}*/
		
		
		// --- 分析最后价后三天的最低价 ---
		int begin = index + offset;
		int end = begin + 4;
		
		if ( end >= stkDailyKLineList.size() ) {
			return ;
		}
		
		// --- mark at least low ---
		StockDailyKLine tmpKLine = null;
		double tmpAtLeastPrice = 9999;
		for ( int i = begin+1 ; i < end ; i++) {
			tmpKLine = stkDailyKLineList.get(i);
			if ( tmpKLine.getLow() < tmpAtLeastPrice) {
				tmpAtLeastPrice = tmpKLine.getLow();
			}
		}
		
		// --- 分析离最230p9成交量之间的最低价 ---
		int j = 1 ;
		int newIndex = 0;
		Date tmpDate = null;
		double tAtLeastPrice = 9999;		
		do {
			
			newIndex = begin+j;
			tmpKLine = stkDailyKLineList.get(newIndex);
			tmpDate =  tmpKLine.getDate();
			
			if ( tmpKLine.getLow() < tAtLeastPrice ) {
				tAtLeastPrice = tmpKLine.getLow();
			}
			
			j+=1;
		} while ( closeDate != null && tmpDate.before( closeDate ) );
		
		//System.out.println("date : " + df.format( tmpDate ) + " , " +tAtLeastPrice + " , " + df.format( closeDate )  );
		
		// --- count to value ---
		//HashMap<String,Integer> mapValueMap = mapCounter.get();
		HashMap<String,Integer> mapValueMap = map;
		if ( res[0] != 0  ) {
			
			countMapValue(tmpAtLeastPrice , res , map);
			countMapValue(tAtLeastPrice , res , atLeastmap);

		}
	}
	
	
	private static void countMapValue(double compareValue  , double[] levelValues , HashMap<String,Integer> mapValueMap) {
		if ( compareValue > levelValues[0] ) {
			int value = mapValueMap.get("A+");
			mapValueMap.put("A+", value+1);
		}
		else if ( compareValue <= levelValues[0] && compareValue > levelValues[1] ) {
			int value = mapValueMap.get("A");
			mapValueMap.put("A", value+1);
		}
		else if ( compareValue <= levelValues[1] && compareValue > levelValues[2] ) {
			int value = mapValueMap.get("B");
			mapValueMap.put("B", value+1);
		}
		else if ( compareValue <= levelValues[2] && compareValue > levelValues[3] ) {
			int value = mapValueMap.get("C");
			mapValueMap.put("C", value+1);
		}
		else if ( compareValue <= levelValues[3] && compareValue > levelValues[4] ) {
			int value = mapValueMap.get("D");
			mapValueMap.put("D", value+1);
		}		
	}
	
	
	public static void reportResult() {
		//HashMap<String,Integer> mapHanle = mapCounter.get();
		System.out.println("this map : " + map);
		
		System.out.println("another handle : " + atLeastmap);
		
		for (Map.Entry<String, Integer> entry : starndmap.entrySet()) {
			System.out.println("keyMap : " + entry.getKey() + " , " + entry.getValue());
		}
		
	}
	
	
	
	private static double[] getLevelArray(List<StockDailyKLine> stkDailyKLineList, int index , int offset) {
		double[] res = new double[5];
		
		
		StockDailyKLine firstKLine = stkDailyKLineList.get( index );
		
		StockDailyKLine lastKLine = stkDailyKLineList.get( index + offset );
		
		
		if ( lastKLine.getLow() > firstKLine.getHigh() ) {
			res[0] = lastKLine.getHigh();
			res[1] = lastKLine.getLow();
			res[2] = firstKLine.getHigh();
			res[3] = firstKLine.getLow();
			res[4] = firstKLine.getLow() * 0.98;
		} 
		else if (lastKLine.getLow() >= firstKLine.getLow() )	{
			if ( lastKLine.getLow() <= firstKLine.getHigh() 
				|| lastKLine.getHigh() <= firstKLine.getHigh() ) {
				res[0] = lastKLine.getHigh();
				res[1] = lastKLine.getLow();
				res[2] = lastKLine.getLow();
				res[3] = firstKLine.getLow();
				res[4] = firstKLine.getLow() * 0.98;
				
			}
			
		}
		else if (lastKLine.getLow() < firstKLine.getLow() 
				&& lastKLine.getHigh() >= firstKLine.getHigh() ) {
			res[0] = lastKLine.getHigh();
			res[1] = lastKLine.getLow();
			res[2] = lastKLine.getLow();
			res[3] = lastKLine.getLow();
			res[4] = lastKLine.getLow() * 0.98;
			
		} 
		else {
			//System.out.println("---- unknow -----");
			//System.out.println(firstKLine.getLow() + " , " + firstKLine.getHigh() );
			//System.out.println(lastKLine.getLow() + " , " + lastKLine.getHigh() );
			//System.exit(1);
			
		}
		
		
		return res;		
	}
	
	

}
